package com.boarsoft.boar.config.drm.listener;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.apache.commons.beanutils.ConvertUtils;

import com.alipay.drm.client.file.ResourceValueChangedEvent;
import com.alipay.drm.client.file.ResourceValueChangedListener;
import com.boarsoft.config.manager.PropConfigManager;

public class DrmConfigChangedListener implements ResourceValueChangedListener {


    @Override
    public void onChanged(ResourceValueChangedEvent resourceValueChangedEvent) {
        Object obj= PropConfigManager.getResource(resourceValueChangedEvent.getResourceId());
        try {
            for (String c : resourceValueChangedEvent.getChangedKeys()) {
                Field f = obj.getClass().getDeclaredField(c);
                f.setAccessible(true);

                try {
                    PropertyDescriptor descriptor = new PropertyDescriptor(f.getName(), obj.getClass());
                    Method writeMethod = descriptor.getWriteMethod();
                    writeMethod.invoke(obj, ConvertUtils.convert((String) resourceValueChangedEvent.getNewValue().get(c),f.getType()));
                } catch (Exception e) {
                    f.set(obj, ConvertUtils.convert((String) resourceValueChangedEvent.getNewValue().get(c),f.getType()));
                }

            }
        }catch(Exception e){
            e.printStackTrace();
        }

    }
}